/************************************************************************/
/* Resources                                                            */
/************************************************************************/
#include "Resources/Tiles/Tile.h"

namespace TD_Resources {
	
	/************************************************************************/
	/* Constructors                                                         */
	/************************************************************************/
	Tile::Tile() {
		m_clip = NULL;
		m_tileType = invalidTile;
	}

	Tile::Tile(const Tile &rhs) {
		m_pImage = rhs.getImage();
		m_tileType = rhs.getType();
		m_clip = rhs.getClipping();
	}

	Tile & Tile::operator=(const Tile &rhs) {
		m_pImage = rhs.getImage();
		m_tileType = rhs.getType();
		m_clip = rhs.getClipping();
		return(*this);
	}

	Tile::Tile(Coord2D tilePosition) : BaseEntity(tilePosition) {
		m_tileSize.x = m_tileSize.y = -1;
	}

	Tile::Tile(Coord2D tilePosition, Coord2D tileSize) : BaseEntity(tilePosition)  {
		m_shapes.push_back(new RectangleShape(tilePosition, tileSize));
		m_tileSize = tileSize;
	}

	Tile::Tile(Coord2D tilePosition, Coord2D tileSize, TileType g_tileType, shared_ptr<SDLImage> rootImg) : BaseEntity(tilePosition) {
		m_shapes.push_back(new RectangleShape(tilePosition, tileSize));
		//Fuck it for instance
		m_tileType = charTile;
		m_tileSize = tileSize;

		m_pImage = rootImg;

		m_clip = NULL;
	}

	Tile::Tile(Coord2D tilePosition, Coord2D tileSize, TileType g_tileType, SDL_Rect *clip, shared_ptr<SDLImage> rootImg) : BaseEntity(tilePosition) {
		/**Instantiate a rectangle shape for the internal collision shapes.*/
		m_shapes.push_back(new RectangleShape(tilePosition, tileSize));

		m_tileType = g_tileType;


		m_tileSize = tileSize;

		m_position = tilePosition;
		m_rotationAngle = 0.0f;

		m_clip = clip;
		
		m_pImage = rootImg;

	}
	Tile::~Tile() {
		
	}

	void Tile::blitSurface(SDL_Surface *const g_destSurface, SDL_Rect *g_pClippingRect /* = NULL */) const {
		SDL_Rect worldOffset;
		worldOffset.x = m_position.x;
		worldOffset.y = m_position.y;
		if(m_pImage != NULL)
		m_pImage->blitSurface(g_destSurface, &worldOffset, g_pClippingRect);
	}

	/************************************************************************/
	/* Rendering                                                            */
	/************************************************************************/	
	inline void Tile::render(SDL_Surface *g_pRenderPane, unsigned int deltaTime) {
		blitSurface(g_pRenderPane, m_clip);
	}
}